package com.prime.tutorial.moviestore.service.logistics;

import com.prime.tutorial.moviestore.Constants;
import com.prime.tutorial.moviestore.dao.DeviceDAO;
import com.prime.tutorial.moviestore.dao.ModelDAO;
import com.prime.tutorial.moviestore.dao.StateDAO;
import com.prime.tutorial.moviestore.domain.Device;
import com.prime.tutorial.moviestore.domain.Model;
import com.prime.tutorial.moviestore.domain.State;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Implementation of {@link LogisticsService}<br/>
 * Created by IntelliJ IDEA.<br/>
 * Users: <br/>
 * Date: 1 бер 2010<br/>
 * Time: 8:25:56 <br/>
 * @author iryna.masyuk
 */

@Service
public class LogisticsServiceImpl implements LogisticsService {

    private ModelDAO modelDAO;
    private StateDAO stateDAO;
    private DeviceDAO deviceDAO;

    @Autowired
    public LogisticsServiceImpl(ModelDAO modelDAO,
                                StateDAO stateDAO,
                                DeviceDAO deviceDAO) {
        this.modelDAO = modelDAO;
        this.stateDAO = stateDAO;
        this.deviceDAO = deviceDAO;
    }

    public List<Model> getDeviceModels(){
        return modelDAO.loadAll();
    };

    public Model findModel(String modelName){
        return modelDAO.findByType(modelName).get(0);
    }

    public State findState(String stateName){
        return stateDAO.findByType(stateName).get(0);
    };

    public void createDevice(Device device){
        //creates device
        deviceDAO.persist(device);
    }

    public void deleteDevice(Device device) {
        //To change body of implemented methods use File | Settings | File Templates.
        deviceDAO.delete(device);
    }

    public List<Device> getDevicesOnStorage(){
        return deviceDAO.findByState(stateDAO.findByType(Constants.STATE_ON_STORAGE));
    };

    public void setApproved(Device d){
        State s = stateDAO.findByType(Constants.STATE_APPROVED_INSTALLATION).get(0);
        d.setState(s);
        deviceDAO.update(d);
    };

    public void updateApproved(){
        //get list of devices planned for installation
        State planned = stateDAO.findByType(Constants.STATE_PLANNED_INSTALLATION).get(0);
        List<Device> l = deviceDAO.findByState(planned);
        //check whether there are corresponding devices on storage
        //if yes - delete devices on storage and set the status to available for installation
        for (Device d: l){

            State onStorage = stateDAO.findByType(Constants.STATE_ON_STORAGE).get(0);
            if (!deviceDAO.findByModelAndState(d.getModel(), onStorage).isEmpty()){
                //if exists - remove device on storage
                Device od=deviceDAO.findByModelAndState(d.getModel(), onStorage).get(0);
                deviceDAO.delete(od);

                //change status
                State a = stateDAO.findByType(Constants.STATE_AVAILABLE_INSTALLATION).get(0);
                d.setState(a);
                deviceDAO.update(d);

            }

        }
    };

    public List<Device> getDevicesPlannedInstallation(){
        return deviceDAO.findByState(stateDAO.findByType(Constants.STATE_PLANNED_INSTALLATION));
    };

    public List<Device> getDevicesAvailableInstallation(){
        return deviceDAO.findByState(stateDAO.findByType(Constants.STATE_AVAILABLE_INSTALLATION));
    };
}
